/* 
 * File:   thresholding.h
 * Author: gustavo
 *
 * Created on November 14, 2011, 2:26 AM
 */

#ifndef THRESHOLD_H
#define	THRESHOLD_H

#ifdef	__cplusplus
extern "C" {
#endif
	
#include "../../matrix/matrix.h"

	/*!
	 * \brief List of thresholding types to fixed threshold.
	 */
	typedef enum _imp_threshold_type ImpThresholdType;
	typedef enum _imp_threshold_adaptive_method ImpThresholdAdaptiveMethod;
	
	/**
	 * To the explanations bellow, consider:
	 * 
	 * \arg \c Th is the threshold value.
	 * \arg \c dst(x,y) a point in the matrix destine.
	 * \arg \c src(x,y) a point int the matrix source.
	 * \arg \c maxVal a maximal value.
	 * \arg \c minVal a minimal value.
	 * 
	 * \sa imp_image_threshold
	 */
	enum _imp_threshold_type {
		/**
		 * \f[
		 * dst(x,y) = \left\{ 
		 * \begin{array}{rl}
		 *		maxVal, & src(x,y) > Th\\
		 *		minVal, & \textrm{otherwise}
		 * \end{array} \right.
		 * \f]
		 */
		IMP_THRESHOLD_BINARY,
		/**
		 * \f[
		 * dst(x,y) = \left\{ 
		 * \begin{array}{rl}
		 *		minVal, & src(x,y) > Th\\
		 *		maxVal, & \textrm{otherwise}
		 * \end{array} \right.
		 * \f]
		 */
		IMP_THRESHOLD_BINARY_INV,
		/**
		 * \f[
		 * dst(x,y) = \left\{ 
		 * \begin{array}{rl}
		 *		maxVal, & src(x,y) > Th\\
		 *		src(x,y), & \textrm{otherwise}
		 * \end{array} \right.
		 * \f]
		 */
		IMP_THRESHOLD_TRUNCATE_UP,
		/**
		 * \f[
		 * dst(x,y) = \left\{ 
		 * \begin{array}{rl}
		 *		src(x,y), & src(x,y) > Th\\
		 *		minVal, & \textrm{otherwise}
		 * \end{array} \right.
		 * \f]
		 */
		IMP_THRESHOLD_TRUNCATE_DOWN,
		/**
		 * \f[
		 * dst(x,y) = \left\{ 
		 * \begin{array}{rl}
		 *		src(x,y), & src(x,y) > Th\\
		 *		0, & \textrm{otherwise}
		 * \end{array} \right.
		 * \f]
		 */
		IMP_THRESHOLD_TO_ZERO,
		/**
		 * \f[
		 * dst(x,y) = \left\{ 
		 * \begin{array}{rl}
		 *		0, & src(x,y) > Th\\
		 *		src(x,y), & \textrm{otherwise}
		 * \end{array} \right.
		 * \f]
		 */
		IMP_THRESHOLD_TO_ZERO_INV
	};
	
	/**
	 * \brief List of supported adaptive threshold algorithms.
	 * 
	 */
	enum _imp_threshold_adaptive_method {
		/**
		 * Wellner extended algorithm.
		 */
		IMP_THRESHOLD_ADAPTIVE_WELLNER_EXTENDED,
		/**
		 * Sauvola adaptive thresholding using integral images.
		 */
		IMP_THRESHOLD_ADAPTIVE_SAUVOLA_FAST
		
	};
	
	void imp_image_threshold(ImpMatrix * Img, real th, real minVal, real maxVal, ImpThresholdType type, ImpMatrix ** BW);
	void imp_image_threshold_adaptive(ImpMatrix * Mat, ImpThresholdAdaptiveMethod method, ImpMatrix ** Thresh);
	
	
	void imp_image_threshold_wellner_extended(ImpMatrix * Img, ImpMatrix ** Thresh);
	void imp_image_threshold_Sauvola_fast(ImpMatrix * Img, int w, real k, ImpMatrix ** Thresh);



#ifdef	__cplusplus
}
#endif

#endif	/* THRESHOLD_H */

